package com.host.tab;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.TabActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.host.tab.chart.AverageTemperatureChart;
import com.host.tab.chart.BudgetPieChart;
import com.host.tab.chart.SalesStackedBarChart;

public class HostTabActivity extends TabActivity {

	String TAG = "HostTabActivity";

	private static final String IncomingCall = "1";
	private static final String OutgoingCall = "2";
	private static final String MissedCall = "3";

	private static String Incoming_text = "来电";
	private static String Outgoing_text = "去电";
	private static String Missed_text = "未接来电";
	
	private RadioGroup filterGroup = null;
	private RadioButton btnIncoming = null;
	private RadioButton btnOutgoing = null;
	private RadioButton btnMissed = null;
	//private RadioButton btnPieChart = null;
	
	private RadioGroup sortGroup = null;
	private RadioButton btnDate = null;
	private RadioButton btnDuration = null;
	private RadioButton btnTimes = null;
	//private RadioButton btnLineChart = null;

	private Cursor cursor_filter;
	
	private Cursor cursor_date_sort;
	private Cursor cursor_duration_sort;
	private Cursor cursor_times_sort;
	
	private ContentResolver resolver = null;
	
	private TabHost tabhost;
	
	//public Filter filter_object = null;
	//public All all_object = null;
	
	private List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> listItemsTab2 = new ArrayList<Map<String, Object>>();
	//private List<Map<String, Object>> listItemsTab3 = new ArrayList<Map<String, Object>>();

	public String temp_name = null;
	public String temp_number = null;
	public String temp_type = null;
	public String temp_date = null;
	public String temp_duration = null;
	public String temp_times = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		//试图用类来封装不同功能。暂时取消
		
		tabhost = getTabHost(); //获取一个TabHost的对象，用来承载Tab。

		LayoutInflater inflater = LayoutInflater.from(this); //定义一个填充布局
		inflater.inflate(R.layout.tab, tabhost.getTabContentView());   //填充TabHost

		tabhost.addTab(tabhost.newTabSpec("Tab1").setIndicator("全部记录")
				.setContent(R.id.tab1)); //添加第一个Tab
		tabhost.addTab(tabhost.newTabSpec("Tab2").setIndicator("筛选")
				.setContent(R.id.tab2)); //添加第二个Tab
		tabhost.addTab(tabhost.newTabSpec("Tab3").setIndicator("排序")
				.setContent(R.id.tab3)); //添加第三个Tab

		//默认一进入程序显示第一个标签卡Tab，“全部记录”
		tabhost.setCurrentTab(0);
		setContentView(tabhost);

		cursor_filter = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
				null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
		startManagingCursor(cursor_filter);
		
		/*List<Map<String, Object>> list_all;
		list_all = all_object.Display_All(cursor);
		RecordAdapter adapter_all = new RecordAdapter(this, list_all);
		ListView listview_all = (ListView) findViewById(R.id.listview1);
		listview_all.setAdapter(adapter_all);
		*/

		//显示全部
		All();
		//筛选
		Filter();
		//排序
		Sort();
		
	
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {   //添加菜单menu
		menu.add(0, 1, 1, R.string.statistics); //添加第一个菜单项--数据统计
		menu.add(0, 2, 2, R.string.exit);   //添加第二个菜单项--退出
		
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}



	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 2){
			finish();
		}
		else if (item.getItemId() == 1){
			Intent intent = new Intent();
			intent.setClass(HostTabActivity.this, VisualizeDisplayActivity.class);
			HostTabActivity.this.startActivity(intent);
		}
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}



	public void Filter(){
	
		filterGroup = (RadioGroup) findViewById(R.id.filterGroup);
		btnIncoming = (RadioButton) findViewById(R.id.incomingButton);
		btnOutgoing = (RadioButton) findViewById(R.id.outgoingButton);
		btnMissed = (RadioButton) findViewById(R.id.missedButton);

		// 过滤标签卡   filter();***********************************************
		filterGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						if (checkedId == btnIncoming.getId()) {
							//Log.i(TAG, "点击了来电@@@@@@@");
							Incoming();
							Toast.makeText(HostTabActivity.this, "我是来电记录",
									Toast.LENGTH_SHORT).show();
						} 
						else if (checkedId == btnOutgoing.getId()) {
							//Log.i(TAG, "点击了拨打！！！！！");
							Outgoing();
							Toast.makeText(HostTabActivity.this, "我是拨打记录",
									Toast.LENGTH_SHORT).show();
						}
						else if (checkedId == btnMissed.getId()) {
							//Log.i(TAG, "点击了未接######");
							Missed();
							Toast.makeText(HostTabActivity.this, "我是未接来电记录",
									Toast.LENGTH_SHORT).show();
						}
						else 
							;
					}
				});
	}
	
	public void Sort(){		// 排序标签卡   Sort();***
		sortGroup = (RadioGroup)findViewById(R.id.sortGroup);
		btnDate = (RadioButton)findViewById(R.id.date_sort_Button);
		btnDuration = (RadioButton)findViewById(R.id.duration_sort_Button);
		btnTimes = (RadioButton)findViewById(R.id.times_sort_Button);

		//按时间排序的SQL拼接
		String projection_date[] ={ "_id", "type", "name", "number", "date", "duration"};
		String selection_date = "1=1 ";
		//按通话时长的SQL拼接
		String projection_duration[] ={ "_id", "type", "name", "number", "date", "duration"};
		String selection_duration = "1=1 ";
		//按通话次数的SQL拼接
		String projection_times[] ={ "_id", "type", "name", "number", "date", "duration", "count(*) as times"};
		String selection_times = "1=1) group by (number ";
				
		resolver = getContentResolver();
		//按照日期，倒叙   SELECT _id, type, name, number, date, duration FROM calls WHERE (1=1 ) ORDER BY date desc
		cursor_date_sort = resolver.query(CallLog.Calls.CONTENT_URI, projection_date, selection_date, null,"date desc");
		//按照通话时间，倒叙  SELECT _id, type, name, number, date, duration FROM calls WHERE (1=1 ) ORDER BY duration desc
		cursor_duration_sort = resolver.query(CallLog.Calls.CONTENT_URI, projection_duration,selection_duration, null,"duration desc");
		//按照通话次数，倒叙  SELECT _id, type, name, number, date, duration, count(*) as times FROM calls WHERE (1=1) group by (number) ORDER BY times desc
		cursor_times_sort = resolver.query(CallLog.Calls.CONTENT_URI, projection_times, selection_times, null, "times desc");
				
		startManagingCursor(cursor_date_sort);
		startManagingCursor(cursor_duration_sort);
		startManagingCursor(cursor_times_sort);
			
		sortGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if (checkedId == btnDate.getId()){
					DateSort(cursor_date_sort);
					Toast.makeText(HostTabActivity.this, "按照日期排序",Toast.LENGTH_SHORT).show();
				}
				else if (checkedId == btnDuration.getId()){
					DurationSort(cursor_duration_sort);
					Toast.makeText(HostTabActivity.this, "按照通话时长排序",Toast.LENGTH_SHORT).show();
				}
				else if (checkedId == btnTimes.getId()){
					TimesSort(cursor_times_sort);
					Toast.makeText(HostTabActivity.this, "按照通话次数排序",Toast.LENGTH_SHORT).show();
				}
				else
					;
			}
		});
	}
	
	//****************************** 绘图函数*******************************
	//折线图
	public void LineChartMonth(){
		AverageTemperatureChart mChart = new AverageTemperatureChart();
		Intent intent = null;
		
		String projection[] = {"date"};
		String selection = "1=1";
		
		Date date = null;
		//用来取出数据库内的时间值，为string类型
		String temp_date_string = null;
		//临时存储返回的日期
		int temp_day = 1;
		
		//用来存储每一天的通话次数
		int month_day_times_int[] = new int[32];
		double month_day_times_double[] = new double[32];
		
		
		int i =0;
		for (i = 0; i< 32; i++){
			month_day_times_int[i] = 0;
			month_day_times_double[i] = 0;
		}
		
		//select date from calls where 1=1
		Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
		startManagingCursor(cursor);
		cursor.moveToFirst();

		while (!cursor.isAfterLast()){
			temp_date_string = cursor.getString(cursor.getColumnIndex("date"));
			date = new Date(Long.parseLong(temp_date_string));
			
			//getYear()返回的是自从1900年开始至今为止多少年。
			//Log.i(TAG, "现在是"+date.getMonth()+"");
			//getMonth()返回值是  0~11，对应1~12月！！！！
			if (date.getMonth() == 2){          //是3月份
				//Log.i(TAG, "进入3月份中！@#！@#！@#");
				temp_day = date.getDate();
				//Log.i(TAG, "当前是第"+temp_day+"天");
				(month_day_times_int[temp_day])++;
				month_day_times_double[temp_day] = new Double(month_day_times_int[temp_day]);
				Log.i(TAG, Double.toString(month_day_times_double[temp_day]));
			}
			
			cursor.moveToNext();
		}
		
		
		List<double[]> list_values = new ArrayList<double[]>();
		list_values.add(month_day_times_double);
		mChart.data_fill(list_values);
		
		intent = mChart.execute(this);
		startActivity(intent);
		
		/*
		SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date_converted = new Date(Long.parseLong(temp_date));

		// date为转换后的日期字符串
		String date = sfd.format(date_converted);
		*/
	}
	
	//柱状图
	public void BarChartMonth(){
		SalesStackedBarChart mChart = new SalesStackedBarChart();
		
		Intent intent = null;
		String projection[] = {"date","duration"};
		String selection = "1=1";
		
		Date date = null;
		//用来取出数据库内的时间值，通话时长值，为string类型
		String temp_date_string = null;
		String temp_duration_string = null;
		double temp_duration_double = 0;
		//临时存储返回的日期
		int temp_day = 1;
		
		//用来存储每一天的通话次数
		//int month_day_times_int[] = new int[32];
		double month_day_duration_double[] = new double[32];
		
		
		int i =0;
		for (i = 0; i< 32; i++){
			//month_day_times_int[i] = 0;
			month_day_duration_double[i] = 0;
		}
		
		//select date from calls where 1=1
		Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
		startManagingCursor(cursor);
		cursor.moveToFirst();

		while (!cursor.isAfterLast()){
			temp_date_string = cursor.getString(cursor.getColumnIndex("date"));
			temp_duration_string = cursor.getString(cursor.getColumnIndex("duration"));
			temp_duration_double = Double.valueOf(temp_duration_string);
			date = new Date(Long.parseLong(temp_date_string));
			
			//getYear()返回的是自从1900年开始至今为止多少年。
			Log.i(TAG, "现在是"+date.getMonth()+"");
			//getMonth()返回值是  0~11，对应1~12月！！！！
			if (date.getMonth() == 3){          //是3月份
				//Log.i(TAG, "进入4月份中！@#！@#！@#");
				temp_day = date.getDate();
				//Log.i(TAG, "当前是第"+temp_day+"天");
				month_day_duration_double[--temp_day] = month_day_duration_double[temp_day]+temp_duration_double;
				//month_day_duration_double[temp_day] = new Double(month_day_times_int[temp_day]);
				//Log.i(TAG, Double.toString(month_day_duration_double[temp_day]));
			}
			
			cursor.moveToNext();
		}
		
		/*
		for (i = 0; i< 32; i++){
			month_day_duration_double[i] = month_day_duration_double[i]/60;
		}
		*/
		
		List<double[]> list_values = new ArrayList<double[]>();
		list_values.add(month_day_duration_double);
		mChart.data_fill(list_values);
		
		intent = mChart.execute(this);
		startActivity(intent);
		
		
	}
	
	//************************下面是Filter过滤子函数**************************

	// 全部通话类型绘图   （饼状图）
	public void PieChartAll(){
		BudgetPieChart mChart = new BudgetPieChart();
		
		String projection[] = {"type","count(*) as times"};
		String selection = "1=1) group by (type";
		
		double times[] = new double[3];
		int i = 0;
		Intent intent = null;
		
		//SELECT type, count(*) as times FROM calls WHERE (1=1) group by (type) ORDER BY null
		Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
		startManagingCursor(cursor);
		
		for (cursor.moveToFirst(), i=0; i<3 ; cursor.moveToNext(),i++){
			Log.i(TAG, "执行完了moveToFirst!!");
			times[i] =  Double.valueOf(cursor.getString(cursor.getColumnIndex("times")));
			//Log.i(TAG, Double.toString(times[i]));   打印从数据库取出的电话次数
			//Log.i(TAG, "这是第"+i+"次");
		}
		//Log.i(TAG, "循环了"+i+"次");
		
		mChart.data_fill(times);
		intent = mChart.execute(this);
		startActivity(intent);
		
	}
	
	
	public void Incoming() {
		List<Map<String, Object>> list_incoming = new ArrayList<Map<String, Object>>();
		cursor_filter.moveToFirst();
		while (!cursor_filter.isAfterLast()) {
			Map<String, Object> listItem = new HashMap<String, Object>();
			temp_type = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.TYPE));
			if (temp_type.equals(IncomingCall)) {
				temp_name = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.CACHED_NAME));
				temp_number = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.NUMBER));
				temp_date = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.DATE));
				temp_duration = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.DURATION));
				// 下面使用的日期类，实现日期的转换，从long类型的时间转换为可读的具体日期。
				SimpleDateFormat sfd = new SimpleDateFormat(
						"yyyy-MM-dd hh:mm:ss");
				Date date_converted = new Date(Long.parseLong(temp_date));
				// date为转换后的日期字符串
				String date = sfd.format(date_converted);
				listItem.put("date", date);
				listItem.put("duration", temp_duration + "秒");
				if (temp_name == null) {
					temp_name = "未知";
				}
				listItem.put("name", temp_name);
				listItem.put("type", Incoming_text);
				listItem.put("number", temp_number);

				list_incoming.add(listItem);
				temp_name = null;
			}
			cursor_filter.moveToNext();
		}
		RecordAdapter adapter_incoming = new RecordAdapter(this, list_incoming);
		ListView listview_incoming = (ListView) findViewById(R.id.listview2);
		listview_incoming.setAdapter(adapter_incoming);
	}

	public void All() {
		List<Map<String, Object>> list_all = new ArrayList<Map<String, Object>>();
		cursor_filter.moveToFirst();
		while (!cursor_filter.isAfterLast()) {
			Map<String, Object> listItem = new HashMap<String, Object>();

			temp_type = cursor_filter.getString(cursor_filter
					.getColumnIndex(CallLog.Calls.TYPE));
			temp_number = cursor_filter.getString(cursor_filter
					.getColumnIndex(CallLog.Calls.NUMBER));
			temp_name = cursor_filter.getString(cursor_filter
					.getColumnIndex(CallLog.Calls.CACHED_NAME));
			temp_date = cursor_filter.getString(cursor_filter
					.getColumnIndex(CallLog.Calls.DATE));
			temp_duration = cursor_filter.getString(cursor_filter
					.getColumnIndex(CallLog.Calls.DURATION));

			// 下面使用的日期类，实现日期的转换，从long类型的时间转换为可读的具体日期。
			SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date date_converted = new Date(Long.parseLong(temp_date));

			// date为转换后的日期字符串
			String date = sfd.format(date_converted);

			listItem.put("date", date);
			listItem.put("duration", temp_duration + "秒");
			if (temp_name == null) {
				temp_name = "未知";
			}
			listItem.put("name", temp_name);
			listItem.put("number", temp_number);
			
			if (temp_type.equals(IncomingCall)) {
				listItem.put("type", Incoming_text);
			} else if (temp_type.equals(OutgoingCall)) {
				listItem.put("type", Outgoing_text);
			} else if (temp_type.equals(MissedCall)) {
				listItem.put("type", Missed_text);
			} else
				;
			list_all.add(listItem);
			temp_name = null;
			cursor_filter.moveToNext();
		}

		RecordAdapter adapter_all = new RecordAdapter(this, list_all);
		ListView listview_all = (ListView) findViewById(R.id.listview1);
		listview_all.setAdapter(adapter_all);

	}

	
	
	public void Outgoing() {
		List<Map<String, Object>> list_outgoing = new ArrayList<Map<String, Object>>();
		cursor_filter.moveToFirst();
		while (!cursor_filter.isAfterLast()) {
			temp_type = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.TYPE));
			if (temp_type.equals(OutgoingCall)) {
				Map<String, Object> listItem = new HashMap<String, Object>();
				temp_number = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.NUMBER));
				temp_name = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.CACHED_NAME));
				temp_date = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.DATE));
				temp_duration = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.DURATION));

				// 下面使用的日期类，实现日期的转换，从long类型的时间转换为可读的具体日期。
				SimpleDateFormat sfd = new SimpleDateFormat(
						"yyyy-MM-dd hh:mm:ss");
				Date date_converted = new Date(Long.parseLong(temp_date));
				// date为转换后的日期字符串
				String date = sfd.format(date_converted);
				listItem.put("date", date);
				listItem.put("duration", temp_duration + "秒");
				if (temp_name == null) {
					temp_name = "未知";
				}
				listItem.put("name", temp_name);
				listItem.put("type", Outgoing_text);
				listItem.put("number", temp_number);

				list_outgoing.add(listItem);
				temp_name = null;
			}
			cursor_filter.moveToNext();
		}
		RecordAdapter adapter_outgoing = new RecordAdapter(this, list_outgoing);
		ListView listview_outgoing = (ListView) findViewById(R.id.listview2);
		listview_outgoing.setAdapter(adapter_outgoing);
	}

	public void Missed() {
		List<Map<String, Object>> list_missed = new ArrayList<Map<String, Object>>();
		cursor_filter.moveToFirst();
		while (!cursor_filter.isAfterLast()) {
			temp_type = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.TYPE));
			if (temp_type.equals(MissedCall)) {
				Map<String, Object> listItem = new HashMap<String, Object>();
				temp_number = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.NUMBER));
				temp_name = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.CACHED_NAME));
				temp_date = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.DATE));
				temp_duration = cursor_filter.getString(cursor_filter.getColumnIndex(CallLog.Calls.DURATION));
				// 下面使用的日期类，实现日期的转换，从long类型的时间转换为可读的具体日期。
				SimpleDateFormat sfd = new SimpleDateFormat(
						"yyyy-MM-dd hh:mm:ss");
				Date date_converted = new Date(Long.parseLong(temp_date));
				// date为转换后的日期字符串
				String date = sfd.format(date_converted);
				listItem.put("date", date);
				listItem.put("duration", temp_duration + "秒");

				if (temp_name == null) {
					temp_name = "未知";
				}
				listItem.put("name", temp_name);
				listItem.put("type", Missed_text);
				listItem.put("number", temp_number);
				list_missed.add(listItem);
				temp_name = null;
			}
			cursor_filter.moveToNext();
		}
		RecordAdapter adapter_missed = new RecordAdapter(this, list_missed);
		ListView listview_missed = (ListView) findViewById(R.id.listview2);
		listview_missed.setAdapter(adapter_missed);
	}

	
	//************************下面是Sort排序子函数**************************
	public void DateSort(Cursor cursor_bak) {
		List<Map<String, Object>> list_date_sort = new ArrayList<Map<String, Object>>();
		cursor_bak.moveToFirst();
		while (!cursor_bak.isAfterLast()) {
			Map<String, Object> listItem = new HashMap<String, Object>();
			temp_type = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.TYPE));
			temp_number = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.NUMBER));
			temp_name = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.CACHED_NAME));
			temp_date = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.DATE));
			temp_duration = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.DURATION));
			// 下面使用的日期类，实现日期的转换，从long类型的时间转换为可读的具体日期。
			SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date date_converted = new Date(Long.parseLong(temp_date));
			// date为转换后的日期字符串
			String date = sfd.format(date_converted);
			listItem.put("date", date);
			listItem.put("duration", temp_duration + "秒");
			listItem.put("number", temp_number);
			if (temp_name == null) {
				temp_name = "未知";
			}
			listItem.put("name", temp_name);
			if (temp_type.equals(IncomingCall)){
				listItem.put("type", Incoming_text);
			}
			else if (temp_type.equals(OutgoingCall)){
				listItem.put("type", Outgoing_text);
			}
			else if(temp_type.equals(MissedCall)){
				listItem.put("type", Missed_text);
			}
			else;
			list_date_sort.add(listItem);
			temp_name = null;
			cursor_bak.moveToNext();
		}
		RecordAdapter adapter_date_sort = new RecordAdapter(this, list_date_sort);
		ListView listview_incoming = (ListView) findViewById(R.id.listview3);
		listview_incoming.setAdapter(adapter_date_sort);
	}
	
	
	public void DurationSort(Cursor cursor_bak) {
		List<Map<String, Object>> list_duration_sort = new ArrayList<Map<String, Object>>();
		cursor_bak.moveToFirst();
		while (!cursor_bak.isAfterLast()) {
			Map<String, Object> listItem = new HashMap<String, Object>();		
			temp_type = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.TYPE));
			temp_number = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.NUMBER));
			temp_name = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.CACHED_NAME));
			temp_date = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.DATE));
			temp_duration = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.DURATION));
			// 下面使用的日期类，实现日期的转换，从long类型的时间转换为可读的具体日期。
			SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date date_converted = new Date(Long.parseLong(temp_date));
			// date为转换后的日期字符串
			String date = sfd.format(date_converted);
			listItem.put("date", date);
			listItem.put("duration", temp_duration + "秒");
			listItem.put("number", temp_number);
			if (temp_name == null) {
				temp_name = "未知";
			}
			listItem.put("name", temp_name);
			if (temp_type.equals(IncomingCall)){
				listItem.put("type", Incoming_text);
			}
			else if (temp_type.equals(OutgoingCall)){
				listItem.put("type", Outgoing_text);
			}
			else if(temp_type.equals(MissedCall)){
				listItem.put("type", Missed_text);
			}
			else;			
			list_duration_sort.add(listItem);
			temp_name = null;
			cursor_bak.moveToNext();
		}
		RecordAdapter adapter_duration_sort = new RecordAdapter(this, list_duration_sort);
		ListView listview_duration_sort = (ListView) findViewById(R.id.listview3);
		listview_duration_sort.setAdapter(adapter_duration_sort);
	}


	public void TimesSort(Cursor cursor_bak) {
		List<Map<String, Object>> list_times_sort = new ArrayList<Map<String, Object>>();
		cursor_bak.moveToFirst();
		while (!cursor_bak.isAfterLast()) {
			Map<String, Object> listItem = new HashMap<String, Object>();
			temp_times = cursor_bak.getString(cursor_bak.getColumnIndex("times"));
			temp_type = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.TYPE));
			temp_number = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.NUMBER));
			temp_name = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.CACHED_NAME));
			temp_date = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.DATE));
			temp_duration = cursor_bak.getString(cursor_bak.getColumnIndex(CallLog.Calls.DURATION));
			// 下面使用的日期类，实现日期的转换，从long类型的时间转换为可读的具体日期。
			SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date date_converted = new Date(Long.parseLong(temp_date));
			// date为转换后的日期字符串
			String date = sfd.format(date_converted);
			listItem.put("date", date);
			listItem.put("duration", temp_duration + "秒");
			listItem.put("number", temp_number);
			if (temp_name == null) {
				temp_name = temp_number;
			}	
			listItem.put("name", temp_name+"("+ temp_times +")");
			if (temp_type.equals(IncomingCall)){
				listItem.put("type", Incoming_text);
			}
			else if (temp_type.equals(OutgoingCall)){
				listItem.put("type", Outgoing_text);
			}
			else if(temp_type.equals(MissedCall)){
				listItem.put("type", Missed_text);
			}
			else;			
			list_times_sort.add(listItem);
			temp_name = null;
			cursor_bak.moveToNext();
		}
		RecordAdapter adapter_times_sort = new RecordAdapter(this, list_times_sort);
		ListView listview_times_sort = (ListView) findViewById(R.id.listview3);
		listview_times_sort.setAdapter(adapter_times_sort);
	}
	
	

	public class RecordAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private List<Map<String, Object>> listItems;

		class ViewHolder {
			public TextView type;
			public TextView name;
			public TextView number;
			public TextView date;
			public TextView duration;
		}

		public RecordAdapter(Context context,
				List<Map<String, Object>> listItems_temp) {

			// this.mContext = context;
			this.mInflater = LayoutInflater.from(context);
			this.listItems = listItems_temp;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			// 显示调用进来的每种（全部、筛选）的listItems的总共条目
			//Log.i(TAG, listItems.size() + "");
			return listItems.size();

		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;

			if (convertView == null) {
				holder = new ViewHolder();
				// convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem, null);
				convertView = mInflater.inflate(R.layout.listitem, null);
				holder.type = (TextView) convertView.findViewById(R.id.type);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.number = (TextView) convertView.findViewById(R.id.number);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.duration = (TextView) convertView.findViewById(R.id.duration);

				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();

			}

			holder.type.setText((String) listItems.get(position).get("type"));
			holder.name.setText((String) listItems.get(position).get("name"));
			holder.number.setText((String) listItems.get(position).get("number"));
			holder.date.setText((String) listItems.get(position).get("date"));
			holder.duration.setText((String) listItems.get(position).get("duration"));

			// holder.number .setText("123455");
			return convertView;

		}

	}

}